package com.ykht.proj.coupon.controller.b;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ykht.proj.common.core.constant.CacheConstants;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.DistributionTypeEnum;
import com.ykht.proj.coupon.entity.*;
import com.ykht.proj.coupon.service.*;
import com.ykht.proj.coupon.vo.FxConfRewardVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @Author: lazyBoy
 * @Date: 2021/2/24 13:38
 */
@RestController
@AllArgsConstructor
@RequestMapping("/b/fxConfPoster")
@Api(value = "/b/fxConfPoster", tags = "推广海报")
public class BxConfPosterController {
	private final FxForwardService fxForwardService;

	private final CnShopService cnShopService;

	private final CnUserExtService cnUserExtService;

	private final FxConfRewardService fxConfRewardService;

	private final FxWaiterExtService fxWaiterExtService;

	private final FxRewardService fxRewardService;

	private final FxConfRewardBaseGlobelService fxConfRewardBaseGlobelService;

	private final FxConfRewardBaseService fxConfRewardBaseService;

	private final FxConfPosterService fxConfPosterService;
	/**
	 * 缓存Api
	 */
	private final RedisTemplate redisTemplate;

	@ApiOperation(value = "推广", notes = "推广")
	@GetMapping("/page")
	public R getCnCouponLogPage() {
		CnShop cnShop = new CnShop().selectById(SecurityUtils.getSjId());
		return R.ok(fxConfPosterService.show(cnShop));
	}

	@ApiOperation(value = "b段跳转到c端根据服务员id分享、赚取佣金", notes = "分享、赚取佣金")
	@PostMapping("/addForwardRecord")
	public R addForwardRecord(@RequestBody FxForward fxForward) {
		try {
			fxForward.setTaskType(1);
			BigDecimal money = BigDecimal.valueOf(0); //设定佣金
			//根据登录人获取服务员id
			FxWaiterExt waiterExt = fxWaiterExtService.getOne(Wrappers.lambdaQuery(FxWaiterExt.class)
					.eq(FxWaiterExt::getLoginId, SecurityUtils.getUserId())
					.eq(FxWaiterExt::getWorkState, 1));
			CnShop one = cnShopService.getById(waiterExt.getShopId());
			fxForward.setForwardTitle(one.getName() + " 分享文章");

			fxForward.setCreateUser(SecurityUtils.getUserId());
			fxForward.setCreateTime(LocalDateTime.now());
			fxForward.setWaiterId(waiterExt.getId());
			//如果平台分销总开关关了、不给钱
			FxConfRewardBaseGlobel fxConfRewardBaseGlobel = fxConfRewardBaseGlobelService.getOne(Wrappers.lambdaQuery(FxConfRewardBaseGlobel.class)
					.eq(FxConfRewardBaseGlobel::getDelFlag, 0)
					.eq(FxConfRewardBaseGlobel::getSwitchs, 0)
					.eq(FxConfRewardBaseGlobel::getForwardArticle, 0)
			);
			System.out.println(fxConfRewardBaseGlobel);
			//如果品牌的分销开关关了、不给钱
			FxConfRewardBase fxConfRewardBase = fxConfRewardBaseService.getOne(Wrappers.lambdaQuery(FxConfRewardBase.class)
					.eq(FxConfRewardBase::getBrandId, waiterExt.getBrandId())
					.eq(FxConfRewardBase::getDelFlag, 0)
					.eq(FxConfRewardBase::getSwitchs, 0)
			);
			/*获取登录人门店以及品牌id*/
			/*根据登录人去获取对应门店的转发配置佣金*/
			/*创建奖励记录对象*/
			FxReward fxReward = new FxReward();
			fxReward.setBrandId(waiterExt.getBrandId());
			fxReward.setShopId(waiterExt.getShopId());
			fxReward.setWaiterId(waiterExt.getId());
			fxReward.setCreateUser(SecurityUtils.getUserId());
			fxReward.setCreateTime(LocalDateTime.now());
			fxReward.setCompanyId(waiterExt.getCompanyId());
			fxReward.setType(fxForward.getTaskType());
			fxReward.setTatId(fxForward.getId());
			FxConfRewardVO confReward = new FxConfRewardVO();
			if (fxConfRewardBaseGlobel != null) {
				if (fxConfRewardBase != null) {
					confReward = fxConfRewardService.findConfByBrandIdAndShopId(waiterExt, DistributionTypeEnum.DISTRIBUTION_TYPE_1.getValue());
					if (confReward != null) {
						int i = fxConfRewardBaseGlobel.getForwardArticleMoney().compareTo(confReward.getBaseMoney());
						if (i > 0) {
							fxReward.setMoney(fxConfRewardBaseGlobel.getForwardArticleMoney());
							fxForward.setForwardMoney(fxConfRewardBaseGlobel.getForwardArticleMoney());
						} else {
							fxReward.setMoney(confReward.getBaseMoney());
							fxForward.setForwardMoney(confReward.getBaseMoney());
						}
					} else {
						fxConfRewardBaseGlobel = fxConfRewardBaseGlobelService.queryValue();
						if (fxConfRewardBaseGlobel != null) {
							fxReward.setMoney(fxConfRewardBaseGlobel.getForwardArticleMoney());
							fxForward.setForwardMoney(fxConfRewardBaseGlobel.getForwardArticleMoney());
						}
					}
				}
				//平台关闭 不给钱
				else {
					fxForward.setForwardMoney(money);
					fxReward.setMoney(money);
				}
			} else {
				fxForward.setForwardMoney(money);
				fxReward.setMoney(money);
			}
			//保存一条转发记录
			fxForwardService.save(fxForward);
			fxReward.setTatId(fxForward.getId());
			//保存一条奖励记录
			fxRewardService.insert(fxReward);
			if (fxReward.getMoney().compareTo(BigDecimal.ZERO) == 1) {
				redisTemplate.opsForValue().set(CacheConstants.SOUND_REMIND + waiterExt.getId(), fxReward.getMoney());
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
		return R.ok();
	}
}
